/**
 * Copyright (C) 2015-2015 iNote.                                      
 * 本系统代码未经授权擅自复制或传播本程序的部分或全部将是非法的.
 * ================================================================
 * ProjectName: iNote
 * FileName: Page.java
 * Created: 2015年4月25日 上午11:49:50 by XQF SUI
 * ================================================================
 */

package com.shishuo.base;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Description: 分页
 * <p>
 * @author XQF SUI
 * <p>
 * Create Date: 2015年4月25日 上午11:49:50
 * <p>
 * @version 1.0
 */
public class Page<T extends BaseEntity> {

    /**
     * 当前页
     */
    private Integer current = 1;
    
    /**
     * 总页数
     */
    private long total;
    
    /**
     * 是否有下一页
     */
    private boolean hasNext;
    
    /**
     * 是否有上一页
     */
    private boolean hasPrevious;
    
    /**
     * 每页显示条数
     */
    private Integer rows = 10;
    
    /**
     * 总数
     */
    private Integer records;
    
    /**
     * 开始条数
     */
    private Integer start = 0;
    
    /**
     * 查询参数
     */
    private Map<String, Object> properties;
    
    /**
     * 查询参数
     */
    private T entity;
    /**
     * 获取的数据
     */
    private List<T> datas;
    
    public Page() {
        
    }
    
    public Page(Integer current, Integer rows) {
        this.setCurrent(current == null ? 1 : current);
        this.setRows(rows == null ? 30 : rows);
    }
    
    public Page(Integer current, Integer rows, T entity) {
        this.setCurrent(current == null ? 1 : current);
        this.setRows(rows == null ? 30 : rows);
        
        this.entity = entity;
    }
    
    public Page(Integer current, Integer rows, Map<String, Object> properties) {
        this.setCurrent(current == null ? 1 : current);
        this.setRows(rows == null ? 30 : rows);
        
        this.properties = properties;
    }
    
    public Integer getCurrent() {
        return current;
    }
    
    public void setCurrent(Integer current) {
        this.current = current;
    }
    
    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public boolean getHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public Integer getRows() {
        return rows;
    }
    
    public void setRows(Integer rows) {
        this.rows = rows;
    }
    
    public long getRecords() {
        return records;
    }
    
    public void setRecords(Integer records) {
        this.records = records;
        
        this.setTotal(records == 0 ? 1 : (int) (records / this.rows + (records % this.rows > 0 ? 1 : 0)));
        this.setCurrent(this.total > this.current ? this.current : (int) this.total);
        this.setStart((this.current -1) * this.rows);
        this.setHasNext(this.total > this.current);
        this.setHasPrevious(this.current > 1);
    }
    
    public Integer getStart() {
        return start;
    }
    
    public void setStart(Integer start) {
        this.start = start;
    }
    
    public Map<String, Object> getProperties() {
        return properties;
    }
    
    public void setProperties(Map<String, Object> properties) {
        this.properties = properties;
    }
    
    public T getEntity() {
        return entity;
    }
    
    public void setEntity(T entity) {
        this.entity = entity;
    }
    
    public List<T> getDatas() {
        return datas;
    }
    
    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

}
